﻿
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using System.Web;
using AliCMS;

namespace UEditor
{
    public static class PathFormatter
    {
        public static string Format(string originFileName, string pathFormat)
        {
            if (String.IsNullOrWhiteSpace(pathFormat))
            {
                pathFormat = "{filename}{rand:6}";
            }

            var invalidPattern = new Regex(@"[\\\/\:\*\?\042\<\>\|]");
            originFileName = invalidPattern.Replace(originFileName, "");

            string extension = Path.GetExtension(originFileName);
            string filename = Path.GetFileNameWithoutExtension(originFileName);

            pathFormat = pathFormat.Replace("{filename}", filename);
            pathFormat = new Regex(@"\{rand(\:?)(\d+)\}", RegexOptions.Compiled).Replace(pathFormat, new MatchEvaluator(delegate(Match match)
            {
                var digit = 6;
                if (match.Groups.Count > 2)
                {
                    digit = Convert.ToInt32(match.Groups[2].Value);
                }
                var rand = new Random();
                return rand.Next((int)Math.Pow(10, digit), (int)Math.Pow(10, digit + 1)).ToString();
            }));

            IManager manager = (IManager)HttpContext.Current.Application["Manager"];
			HttpRequest request = HttpContext.Current.Request;

			//根目录
			string lang = request["Lang"];
			int instance = Convert.ToInt32(request["Instance"]);
			string root = request["Root"];
			if (!String.IsNullOrEmpty(root))
			{
				if (!root.StartsWith("/"))
					root = manager.GetDataPath(instance, lang) + root.TrimEnd('/') + "/";
			}
			else
			{
				root = manager.GetDataPath(instance, lang);
			}

			//子实例或公司ID
			string subInstanceId = request["SubInstanceID"];
			if (!String.IsNullOrEmpty(subInstanceId))
			{
				root += Regex.Replace(subInstanceId.Replace("-", ""), "^([0-9a-f]{3})([0-9a-f]{3})(.*)$", "$1/$2/$3/");
			}

			//子目录
			string folder = request["Folder"];
			if (request["From"] == "Editor")
			{
				string type = "";
				if (request["action"] == "uploadimage" || request["action"] == "uploadscrawl")
					type = "image";
				else if (request["action"] == "uploadvideo")
					type = "video";
				folder = "editor/" + type + "/" + folder;
			}

			pathFormat = pathFormat.Replace("{root}", root);
			pathFormat = pathFormat.Replace("{folder}", folder);
			pathFormat = pathFormat.Replace("{time}", DateTime.Now.Ticks.ToString());
			pathFormat = pathFormat.Replace("{yyyy}", DateTime.Now.Year.ToString());
			pathFormat = pathFormat.Replace("{yy}", (DateTime.Now.Year % 100).ToString("D2"));
			pathFormat = pathFormat.Replace("{mm}", DateTime.Now.Month.ToString("D2"));
			pathFormat = pathFormat.Replace("{dd}", DateTime.Now.Day.ToString("D2"));
			pathFormat = pathFormat.Replace("{hh}", DateTime.Now.Hour.ToString("D2"));
			pathFormat = pathFormat.Replace("{ii}", DateTime.Now.Minute.ToString("D2"));
			pathFormat = pathFormat.Replace("{ss}", DateTime.Now.Second.ToString("D2"));
			pathFormat = pathFormat.Replace("//", "/");

            return pathFormat + extension;
        }
    }
}